/*
 * @(#) ImportUtility.java 2012-7-26 下午01:31:59
 *
 * Copyright 2012 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.mesd.killer.importer;

import java.util.ArrayList;

public class ImportUtility
{
	// constants
	public static final String SEGMENT_SEP = "\\$\\$";

	public static final String GROUP_SEP = "\\$\\$\\$\\$";

	public static final String RECORD_SEP = "!";

	public static final String FIELD_SEP = "\\^";

	public static final int SCHEDULE_RECORD_SEG = 1;

	public static final int SCHEDULE_ROUND_SEG = 0;

	public static final int SCHEDULE_ROUND = 0;

	public static final int SCHEDULE_PLAYING_TIME = 1;

	public static final int SCHEDULE_SIMPLIFIED_HOMETEAM_NAME = 2;

	public static final int SCHEDULE_TRADITIONAL_HOMETEAM_NAME = 3;

	public static final int SCHEDULE_SIMPLIFIED_AWAYTEAM_NAME = 4;

	public static final int SCHEDULE_TRADITIONAL_AWAYTEAM_NAME = 5;

	public static final int SCHEDULE_STATE = 6;

	public static final int SCHEDULE_HOMETEAM_SCORE = 7;

	public static final int SCHEDULE_AWAYTEAM_SCORE = 8;

	public static final int SCHEDULE_HOMETEAM_HALF_SCORE = 9;

	public static final int SCHEDULE_AWAY_HALF_SCORE = 10;

	public static final int SCHEDULE_COUNT = 11;

	public static final int YAPEI_BETCOMPANY_NAME = 0;

	public static final int YAPEI_BETCOMPANY_ID = 1;

	public static final int YAPEI_HOME_CHUPEI = 2;

	public static final int YAPEI_CHUPAN = 3;

	public static final int YAPEI_AWAY_CHUPEI = 4;

	public static final int YAPEI_HOME_JISHI = 5;

	public static final int YAPEI_JISHI = 6;

	public static final int YAPEI_AWAY_JISHI = 7;

	public static final int OUPEI_BETCOMPANY_NAME = 0;

	public static final int OUPEI_BETCOMPANY_ID = 1;

	public static final int OUPEI_CHUPEI_WIN = 2;

	public static final int OUPEI_CHUPEI_DRAW = 3;

	public static final int OUPEI_CHUPEI_LOST = 4;

	public static final int OUPEI_JISHI_WIN = 5;

	public static final int OUPEI_JISHI_DRAW = 6;

	public static final int OUPEI_JISHI_LOST = 7;

	public static final int DAXIAO_BETCOMPANY_NAME = 0;

	public static final int DAXIAO_BETCOMPANY_ID = 1;

	public static final int DAXIAO_DA_CHUPEI = 2;

	public static final int DAXIAO_CHUPAN = 3;

	public static final int DAXIAO_XIAO_CHUPEI = 4;

	public static final int DAXIAO_DA_JISHI = 5;

	public static final int DAXIAO_JISHI = 6;

	public static final int DAXIAO_XIAO_JISHI = 7;

	public static ArrayList parseRequestString(String inputString)
	{
		if (inputString == null)
			return null;
		String[] recordArray = inputString.split(RECORD_SEP);
		ArrayList list = new ArrayList();
		for (int i = 0; i < recordArray.length; i++)
		{
			String record = recordArray[i];
			String[] fieldsArray = record.split(FIELD_SEP);
			list.add(
				i, fieldsArray);
		}
		return list;
	}

	public static ArrayList parseRequestStringWithSegment(String inputString, boolean isCup)
	{
		if (inputString == null)
			return null;
		String[] segmentArray = inputString.split(SEGMENT_SEP);
		int a = 0;
		if (isCup)
		{
			segmentArray = inputString.split(GROUP_SEP);
			if (segmentArray.length <= 1)
			{
				segmentArray = inputString.split(SEGMENT_SEP);
				a = 1;
			}
		}
		if (segmentArray.length <= 1)
			return null;
		String segmentString = segmentArray[1];
		if (a == 1 && segmentArray.length == 3)
		{
			segmentString = segmentArray[2];
		}
		// System.out.println(segmentString);
		ArrayList recordArray = parseRequestString(segmentString);
		return recordArray;
	}

	public static int getMatchCount(String inputString)
	{
		if (inputString == null)
			return 0;
		String[] segmentArray = inputString.split(SEGMENT_SEP);
		return Integer.parseInt(segmentArray[0]);
	}

	public static ArrayList getScheduleObjects(String inputString, boolean isCup, String groupId)
	{
		ArrayList recordArray = parseRequestStringWithSegment(
			inputString, isCup);
		if (recordArray == null)
			return null;
		int len = recordArray.size();
		if (len == 0)
		{
			return null;
		}
		ArrayList scheduleObjects = new ArrayList();
		try
		{
			for (int i = 0; i < recordArray.size(); i++)
			{
				String[] record = (String[]) recordArray.get(i);
				if (record.length < 9)
					continue;
				String homeTeamHalfScore = "0";
				String awayHalfScore = "0";
				if (isCup)
				{
					record[SCHEDULE_ROUND] = groupId;
				}
				if (record.length == 11)
				{
					homeTeamHalfScore = record[SCHEDULE_HOMETEAM_HALF_SCORE];
					awayHalfScore = record[SCHEDULE_AWAY_HALF_SCORE];
				}
				ScheduleObject scheduleObject = new ScheduleObject(record[SCHEDULE_ROUND],
					record[SCHEDULE_PLAYING_TIME], record[SCHEDULE_SIMPLIFIED_HOMETEAM_NAME],
					record[SCHEDULE_TRADITIONAL_HOMETEAM_NAME],
					record[SCHEDULE_SIMPLIFIED_AWAYTEAM_NAME],
					record[SCHEDULE_TRADITIONAL_AWAYTEAM_NAME], record[SCHEDULE_STATE],
					record[SCHEDULE_HOMETEAM_SCORE], record[SCHEDULE_AWAYTEAM_SCORE],
					homeTeamHalfScore, awayHalfScore);

				if (scheduleObject != null)
				{
					scheduleObjects.add(scheduleObject);
				}

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return scheduleObjects;
	}

	public static ArrayList getYaPeiObjects(String inputString)
	{
		ArrayList recordArray = parseRequestString(inputString);
		if (recordArray == null)
			return null;
		int len = recordArray.size();
		if (len == 0)
		{
			return null;
		}
		ArrayList yaPeiObjects = new ArrayList();
		for (int i = 0; i < recordArray.size(); i++)
		{
			String[] record = (String[]) recordArray.get(i);
			YapeiObject yaPeiObject = new YapeiObject(record[YAPEI_BETCOMPANY_NAME],
				record[YAPEI_BETCOMPANY_ID], Double.parseDouble(record[YAPEI_HOME_CHUPEI]),
				record[YAPEI_CHUPAN], Double.parseDouble(record[YAPEI_AWAY_CHUPEI]),
				Double.parseDouble(record[YAPEI_HOME_JISHI]), record[YAPEI_JISHI],
				Double.parseDouble(record[YAPEI_AWAY_JISHI]));
			yaPeiObjects.add(yaPeiObject);
		}
		return yaPeiObjects;
	}

	public static ArrayList getOuPeiObjects(String inputString)
	{
		ArrayList recordArray = parseRequestString(inputString);
		if (recordArray == null)
			return null;
		int len = recordArray.size();
		if (len == 0)
		{
			return null;
		}
		ArrayList ouPeiObjects = new ArrayList();
		for (int i = 0; i < recordArray.size(); i++)
		{
			String[] record = (String[]) recordArray.get(i);
			OupeiObject ouPeiObject = new OupeiObject(record[OUPEI_BETCOMPANY_NAME],
				record[OUPEI_BETCOMPANY_ID], Double.parseDouble(record[OUPEI_CHUPEI_WIN]),
				Double.parseDouble(record[OUPEI_CHUPEI_DRAW]),
				Double.parseDouble(record[OUPEI_CHUPEI_LOST]),
				Double.parseDouble(record[OUPEI_JISHI_WIN]),
				Double.parseDouble(record[OUPEI_JISHI_DRAW]),
				Double.parseDouble(record[OUPEI_JISHI_LOST]));
			ouPeiObjects.add(ouPeiObject);
		}
		return ouPeiObjects;
	}

	public static ArrayList getDaXiaoObjects(String inputString)
	{
		ArrayList recordArray = parseRequestString(inputString);
		if (recordArray == null)
			return null;
		int len = recordArray.size();
		if (len == 0)
		{
			return null;
		}
		ArrayList daXiaoObjects = new ArrayList();
		for (int i = 0; i < recordArray.size(); i++)
		{
			String[] record = (String[]) recordArray.get(i);
			DaXiaoObject daXiaoObject = new DaXiaoObject(record[DAXIAO_BETCOMPANY_NAME],
				record[DAXIAO_BETCOMPANY_ID], Double.parseDouble(record[DAXIAO_DA_CHUPEI]),
				record[DAXIAO_CHUPAN], Double.parseDouble(record[DAXIAO_XIAO_CHUPEI]),
				Double.parseDouble(record[DAXIAO_DA_JISHI]), record[DAXIAO_JISHI],
				Double.parseDouble(record[DAXIAO_XIAO_JISHI]));
			daXiaoObjects.add(daXiaoObject);
		}
		return daXiaoObjects;
	}
}
